<template>
  <!-- PAGE CONTENT BEGINS -->
  <div>
    <h4 class="lighter">
      <i class="ace-icon fa fa-hand-o-right"/>
      <router-link to="/business/course" class="pink">{{course.name}}</router-link>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <i class="ace-icon fa fa-hand-o-right"/>
      <router-link to="/business/chapter" class="pink">{{chapter.name}}</router-link>
    </h4>
    <hr>
    <p>
      <button v-on:click="add()" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-edit bigger-120 blue"></i>
        新增
      </button>
      &nbsp;
      <button v-on:click="list(1)" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-refresh bigger-120 blue"></i>
        刷新
      </button>
    </p>
    <pagination ref="pagination" v-bind:list="list"></pagination>
    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>
        <th> ID </th>
        <th> 标题 </th>
<!--        <th> 课程 </th>-->
<!--        <th> 大章 </th>-->
        <th> 视频 </th>
        <th> 时长 </th>
        <th> 收费 </th>
        <th> 顺序 </th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="section in sections" :key="section.num">
          <td> {{section.id}} </td>
          <td> {{section.title}} </td>
<!--          <td> {{section.courseId}} </td>-->
<!--          <td> {{section.chapterId}} </td>-->
          <td> {{section.video}} </td>
          <td> {{section.time | formatSecond}} </td>
          <td> {{SECTION_CHARGE | optionKV(section.charge)}} </td>
          <td> {{section.sort}} </td>
        <td>
          <div class="hidden-sm hidden-xs btn-group">
            <button v-on:click="edit(section)" class="btn btn-xs btn-info">
              <i class="ace-icon fa fa-pencil bigger-120"></i>
            </button>

            <button v-on:click="del(section.id)" class="btn btn-xs btn-danger">
              <i class="ace-icon fa fa-trash-o bigger-120"></i>
            </button>
          </div>

          <div class="hidden-md hidden-lg">
            <div class="inline pos-rel">
              <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
              </button>

              <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                <li>
                  <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
                    <span class="blue">
                      <i class="ace-icon fa fa-search-plus bigger-120"></i>
                    </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
                    <span class="green">
                      <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                    </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
                    <span class="red">
                      <i class="ace-icon fa fa-trash-o bigger-120"></i>
                    </span>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </td>
      </tr>
      </tbody>
    </table>
    <!--模态框-->
    <div class="modal fade" id="myFormModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">表单</h4>
          </div>
          <div class="modal-body">
            <!--表格-->
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">标题</label>
                <div class="col-sm-10">
                  <input v-model="section.title" class="form-control" placeholder="输入标题"/>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">课程</label>
                <div class="col-sm-10">
                  <p class="form-control-static" >{{course.name}}</p>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">大章</label>
                <div class="col-sm-10">
                  <p class="form-control-static" >{{ chapter.name }}</p>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">视频</label>
                <div class="col-sm-10">
                  <big-file v-bind:suffixs=" [ 'mp4' ] "
                        v-bind:use="FILE_USE.COURSE.key"
                        v-bind:text="'上传视频'"
                        v-bind:after-upload="afterUpload"
                        v-bind:input-id=" 'image-upload' "></big-file>
                  <div v-show="section.video" class="row">
                    <div class="col-md-10">
                      <player ref="player"></player>
<!--                      <video v-bind:src="section.video" id="video" controls="controls"></video>-->
                    </div>
                  </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">时长</label>
                <div class="col-sm-10">
                  <input disabled v-model="section.time" class="form-control" placeholder="输入时长"/>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">收费</label>
                <div class="col-sm-10">
                  <select v-model="section.charge" class="form-control">
                    <option v-for="o in SECTION_CHARGE" v-bind:value="o.key" :key="o.num">{{o.value}}</option>
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">顺序</label>
                <div class="col-sm-10">
                  <input v-model="section.sort" class="form-control" placeholder="输入顺序"/>
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" v-on:click="save()">保存</button>
          </div>
        </div><!-- /.modal-content-->
      </div><!-- /.modal-dialog-->
    </div><!-- /.modal-->
  </div>
</template>
<script>
import Pagination from "../../components/paginations";
import BigFile from "../../components/big-file";
import Player from "@/components/player";
export default {
  name: 'business-section',
  components: {Player, Pagination,BigFile},
  data: function (){
    return{
      //映射表单数据
      section: {},
      sections: [],
      SECTION_CHARGE: SECTION_CHARGE,
      FILE_USE:FILE_USE,
      course:{},
      chapter:{},
    }
  },
  mounted:function (){
    let _this = this;
    _this.$parent.activeSidebar("business-course-sidebar");
    let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
    let chapter = SessionStorage.get(SESSION_KEY_CHAPTER) || {};
    if (Tool.isEmpty(course) || Tool.isEmpty(chapter)) {
      _this.$router.push("/welcome");
    }
    _this.course = course;
    _this.chapter = chapter;
    _this.list(1);
  },
  methods:{

    add() {
      let _this = this;
      _this.section = {};
      $("#myFormModal").modal("show");
    },

    edit(section) {
      let _this = this;
      _this.section = $.extend({},section);
      $("#myFormModal").modal("show");
    },
    /**查询*/
    list(page){
      let _this = this;
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/section/list',{
        page:page,
        size:_this.$refs.pagination.size,
        courseId:_this.course.id,
        chapterId:_this.chapter.id,
      }).then((response)=>{
        Loading.hide();
        console.log("查询小节列表结果：",response);
        let resp = response.data;
        _this.sections = resp.content.list;
        _this.$refs.pagination.render(page,resp.content.total);
      })
    },
    /**保存*/
    save(){
      let _this = this;
      /**保存校验*/
      if(1 != 1
      || !Validator.require(_this.section.title,"标题")
      || !Validator.length(_this.section.title,"标题",1,50)
      || !Validator.length(_this.section.video,"视频",1,200)
      ){
        return;
      }
      _this.section.courseId = _this.course.id;
      _this.section.chapterId = _this.chapter.id;
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/section/save',_this.section)
      .then((response)=>{
        console.log("保存小节列表结果：",response);
        let resp = response.data;
        if(resp.success){
          $("#myFormModal").modal("hide");
          _this.list(1);
          toast.success("保存成功！");
        }else{
          toast.warning(resp.message);
        }


      })
    },
    /**删除*/
    del(id){
      let _this = this;
      //弹出框
      Confirm.show("删除小节后不可恢复，您是否检查删除!",function (){
        _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/section/delete/'+ id )
            .then((response)=>{
              console.log("删除小节列表结果：",response);
              let resp = response.data;
              if(resp.success){
                _this.list(1);
                toast.success("删除成功！");
              }
            });
      })
    },
    /**上传组件执行后回调函数*/
    afterUpload(resp) {
      let _this = this;
      let video = resp.content.path;
      _this.section.video = video;
      _this.getTime();
      _this.$refs.player.playUrl(video);
    },
    /**获取上传视频的时长*/
    getTime(){
      let _this = this;
      let ele = document.getElementById("video");
      _this.section.time = parseInt(ele.duration,10);

    }
  }
}
</script>
<style scoped>
  video{
    width: 100%;
    height: auto;
    margin-top: 10px;
  }
</style>